package com.lianzhihui.romeex.ui.wallet;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.bumptech.glide.Glide;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.TradeRecordBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.mvp.presenter.TradeDetailsPresenter;
import com.lianzhihui.romeex.mvp.view.TradeDetailsView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.ui.base.CustomWebActivity;
import com.lianzhihui.romeex.utils.CoinUtils;
import com.lianzhihui.romeex.utils.DateUtil;
import com.lianzhihui.romeex.utils.NumberUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.wallet
 * @ClassName: ReceiveRecordDetails
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/11 17:41
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/11 17:41
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class TradeRecordDetailsActivity extends BaseActivity implements TradeDetailsView {

    @BindView(R.id.include_top_iv_back)
    ImageView include_top_iv_back;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout include_top_lin_newback;
    @BindView(R.id.include_top_tv_title)
    TextView include_top_tv_title;
    @BindView(R.id.include_top_iv_right_second)
    ImageView include_top_iv_right_second;
    @BindView(R.id.include_top_tv_right)
    TextView include_top_tv_right;
    @BindView(R.id.include_top_lin_right)
    LinearLayout include_top_lin_right;
    @BindView(R.id.include_top_lin_background)
    LinearLayout include_top_lin_background;
    @BindView(R.id.app_top_bar)
    LinearLayout app_top_bar;
    @BindView(R.id.iv_icon)
    ImageView iv_icon;
    @BindView(R.id.rl_type_range)
    RelativeLayout rl_type_range;
    @BindView(R.id.rl_fee_range)
    RelativeLayout rl_fee_range;
    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.tv_sum)
    TextView tv_sum;
    @BindView(R.id.tv_state)
    TextView tv_state;
    @BindView(R.id.countType)
    TextView countType;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_receive_address)
    TextView tv_receive_address;
    @BindView(R.id.tv_address_to)
    TextView tv_address_to;
    @BindView(R.id.tv_payment_address)
    TextView tv_payment_address;
    @BindView(R.id.tv_address_from)
    TextView tv_address_from;
    @BindView(R.id.tv_id_number)
    TextView tv_id_number;
    @BindView(R.id.tv_hash_id)
    TextView tv_hash_id;
    @BindView(R.id.tv_remark)
    TextView tv_remark;
    @BindView(R.id.tv_fee)
    TextView tv_fee;
    @BindView(R.id.bt_cross_brower)
    Button bt_cross_brower;
    @BindView(R.id.bt_brower)
    Button bt_brower;
    @BindView(R.id.cardview)
    CardView cardview;

    TradeRecordBean.DetailBean details;

    TradeDetailsPresenter presenter;
    String hash;
    private int chainType;
    private String tradeType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_receive_details;
    }

    @Override
    protected void initView() {
        presenter = new TradeDetailsPresenter(this, this);
        include_top_lin_background.setBackgroundColor(getResources().getColor(R.color.trade_bg_F4F5F7_color));

        hash = getIntent().getStringExtra(AppConstants.RECORD_DETAILS_KEY);

        chainType = getIntent().getIntExtra("chainType", 0);

        countType.setText("普通收款");

        if (chainType == 4) {
            rl_type_range.setVisibility(View.VISIBLE);
        } else {
            rl_type_range.setVisibility(View.GONE);
        }
    }

    private void getData() {
        showLoading("");
        presenter.getTradeDetails(hash);
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    public void onGetTradeDetails(TradeRecordBean.DetailBean bean) {
        dismissLoading();
        details = bean;
        loadData(details);
    }

    @SuppressLint("SetTextI18n")
    private void loadData(TradeRecordBean.DetailBean bean) {
        if (!this.isDestroyed()) {
            Glide.with(this).load(CoinUtils.getTransactionTypeIcon(bean)).into(iv_icon);
//        LogUtil.error("ramon", "detail bean:" + bean.toString());

            //icon
            //状态 按理只有成功，不然不会存在于记录中
            tv_state.setText(bean.getTradeTypeStr());
            //数量
            tv_sum.setText(NumberUtils.big8(Double.parseDouble(bean.getAmount())) + bean.getCoinName());
            //时间
            tv_time.setText(DateUtil.sec2Date(bean.getTimestamp()));
            if (bean.chainType == 2) {
                tv_fee.setText(bean.getFee() + " " + bean.getFeeCoinName());
                rl_fee_range.setVisibility(View.VISIBLE);
            } else {
                rl_fee_range.setVisibility(View.GONE);
            }
            //收款地址
            tv_address_to.setText(bean.getTargetAddress());
            //付款地址
            tv_address_from.setText(bean.getSourceAddress());
            //hash
            tv_hash_id.setText(bean.getTransactionHashcode());
            //备注
            tv_remark.setText(bean.getRemark());
            String crossUrl = bean.getCrossBrowserUri();
            if (TextUtils.isEmpty(crossUrl)) {
                //隐藏跨链 按钮
                bt_cross_brower.setVisibility(View.GONE);
            }
            String browerUrl = bean.getBrowserUri();
            if (TextUtils.isEmpty(browerUrl)) {
                bt_brower.setVisibility(View.GONE);
            }

            setTitle(bean.getTitle());
        }


    }

    @Override
    public void onGetTradeDetailsFail(Object obj) {
        toast(getString(R.string.get_data_fail));
    }

    @OnClick({R.id.tv_address_to, R.id.tv_address_from, R.id.tv_hash_id, R.id.bt_cross_brower, R.id.bt_brower})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_address_to:
                copyString(tv_address_to.getText().toString());
                break;
            case R.id.tv_address_from:
                copyString(tv_address_from.getText().toString());
                break;
            case R.id.tv_hash_id:
                copyString(tv_hash_id.getText().toString());
                break;
            case R.id.bt_cross_brower:
                Intent intent1 = new Intent(this, CustomWebActivity.class);
                intent1.putExtra("url", details.getCrossBrowserUri());
//                intent1.putExtra("title", getResources().getString(R.string.text_user_agreement));
                intent1.putExtra("isHideTitle", false);
                startActivity(intent1);
                break;
            case R.id.bt_brower:
                Intent intent2 = new Intent(this, CustomWebActivity.class);
                intent2.putExtra("url", details.getBrowserUri());
//                intent1.putExtra("title", getResources().getString(R.string.text_user_agreement));
                intent2.putExtra("isHideTitle", false);
                startActivity(intent2);
                break;
            default:
                break;
        }
    }

}
